<!--
 * @Author: your name
 * @Date: 2021-08-16 17:40:41
 * @LastEditTime: 2021-08-22 21:40:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \orderSystems\src\views\menuMag\search.vue
-->
<template>
  <!-- 搜索组件 -->
  <div id="search">
    <div class="search-container">
      <div class="search-title">查询</div>
      <el-form :inline="true" :model="formInline" class="form-search">
        <el-form-item label="搜索：" class="search-item">
          <el-input
            clearable
            v-model="formInline.search"
            placeholder="可以根据菜品类别进行搜索"
          ></el-input>
        </el-form-item>
        <el-form-item class="search-item">
          <el-button type="success" icon="el-icon-search" @click="onSearch"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import bus from "../../EventBus";
export default {
  data() {
    return {
      formInline: {
        search: "", //要搜索的内容
      },
      searchstatus: false, //默认false,表示没有点击查询
      tableData: [], //查询数据的数组
    };
  },
  props: {
    moudleName: {
      type: String,
      default: "",
    },
  },
  methods: {
    //点击搜索的函数
    async onSearch() {
      switch (this.moudleName) {
        case "菜品类别":
          const { data: res } = await this.axios.post("/dishesCategory/list", {
            name: this.formInline.search,
          });
          console.log("查询数据", res);
          if (res.code == 200) {
            this.searchstatus = true;
            this.tableData = res.data;
            // bus.$emit("shareTableData", {tableData:this.tableData,searchstatus:this.searchstatus});
            this.$emit("shareSearchStatus", {
              tableData:this.tableData,
              totalCount:res.count,
              searchStatus:this.searchstatus,
              name:this.formInline.search
            });
          }
          break;
        case "菜品单位":
          const { data: res1 } = await this.axios.post("/dishUnit/list", {
            name: this.formInline.search,
          });
          console.log("菜品单位数据", res1);
          if (res1.code == 200) {
            this.searchstatus = true;
            this.tableData = res1.data;
            // console.log('菜品单位',res1.data);
             this.$emit("shareSearchStatus", {
              tableData:this.tableData,
              totalCount:res1.count,
              searchStatus:this.searchstatus,
              name:this.formInline.search
            });
            /* bus.$emit("shareTableData", this.tableData);
            this.$emit("shareSearchStatus", this.searchstatus); */
          }
          break;
          case "菜品口味":
          const { data: res2 } = await this.axios.post("/dishTaste/list", {
            name: this.formInline.search,
          });
          console.log("菜品口味数据", res2);
          if (res2.code == 200) {
            this.searchstatus = true;
            this.tableData = res2.data;
            //将表格里的属性名taste修改为表格数据中定义的名字
            this.tableData= JSON.parse(JSON.stringify(this.tableData).replace(/taste/g,"category"));
            // console.log('菜品单位',res1.data);
             this.$emit("shareSearchStatus", {
              tableData:this.tableData,
              totalCount:res2.count,
              searchStatus:this.searchstatus,
              name:this.formInline.search
            });
            /* bus.$emit("shareTableData", this.tableData);
            this.$emit("shareSearchStatus", this.searchstatus); */
          }
          break;
          case "规格":
          const { data: res3 } = await this.axios.post("/dishSpecification/list", {
            name: this.formInline.search,
          });
          console.log("菜品规格数据", res3);
          if (res3.code == 200) {
            this.searchstatus = true;
            this.tableData = res3.data;
            //将表格里的属性名specification修改为表格数据中定义的名字
            this.tableData= JSON.parse(JSON.stringify(this.tableData).replace(/specification/g,"category"));
            // console.log('菜品单位',res1.data);
             this.$emit("shareSearchStatus", {
              tableData:this.tableData,
              totalCount:res3.count,
              searchStatus:this.searchstatus,
              name:this.formInline.search
            });
            /* bus.$emit("shareTableData", this.tableData);
            this.$emit("shareSearchStatus", this.searchstatus); */
          }
          break;
      }
    },
  },
};
</script>

<style lang='less' scoped>
.search-container {
  background-color: #fff;
  //   height: 8rem;
  border: 0.1rem solid #dbe0e9;
  .search-title {
    height: 4rem;
    background-color: #f2f2f2;
    box-sizing: border-box;
    border-bottom: 0.1rem solid #dbe0e9;
    line-height: 4rem;
    font-size: 1.4rem;
    padding-left: 3rem;
    font-weight: 500;
    color: #666666;
  }
  .form-search {
    margin: 1rem;
    padding: 1.5rem;
    background-color: #f5f9fc;
    box-sizing: border-box;
    .search-item {
      margin-bottom: 0;
    }
  }
}
</style>